<?php
/**
 * TomatoCMS
 *
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 *
 * @copyright    Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license        http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version     $Id: ArticleIcons.php 3060 2010-05-26 07:56:08Z huuphuoc $
 * @since        2.0.3
 */

/**
 * This helper show items in cart
 */
class Shoppingcart_View_Helper_ItemsCart extends Zend_View_Helper_Abstract
{
    public function itemsCart()
    {
        /**
        * Get cart id from cookie
        **/
        $card_id = $this->buildCookie();

        /**
        * Get connection to database
        **/
        $conn = Tomato_Db_Connection::factory()->getMasterConnection();

        /**
        * Cart models access to data
        **/
        $cartDao = Tomato_Model_Dao_Factory::getInstance()->setModule('shoppingcart')->getCartDao();
        $cartDao->setDbConnection($conn);

        /**
         * Set current language for cart modles
        **/
        //$cartDao->setLang($lang);

        /**
         * Get cart detail info
        **/
        $cartDetails = $cartDao->getDetailById($card_id);
        $numItems = 0;
        foreach ($cartDetails as $row){
        	if(!$row->cart_parent_id){
        		$numItems += 1;
        	}
        }
        $this->view->assign('numItems', $numItems);

        $this->view->addScriptPath(TOMATO_APP_DIR . DS . 'modules' . DS . 'shoppingcart' . DS . 'views' . DS . 'scripts');
        return $this->view->render('_partial/_itemsCart.phtml');
    }

    /**
     * Build cookie
     * Return cart_id
     * @return int
    **/
    public function buildCookie(){
        /**
        * Get connection to database
        **/
        $conn = Tomato_Db_Connection::factory()->getMasterConnection();

        /**
        * Cart models access to data
        **/
        $cartDao = Tomato_Model_Dao_Factory::getInstance()->setModule('shoppingcart')->getCartDao();
        $cartDao->setDbConnection($conn);

        if(!isset($_COOKIE['cart_cookie'])){
            /**
             * Get all cart
             * $arrCarts : Array cart id
            **/
            $carts = $cartDao->getCart();
            $arrCarts = array();
            foreach($carts as $c_row){
                $arrCarts[] = $c_row->cart_id;
            }

            /**
             * Create array 1 element to compare $arrCarts
             * Return $arrDiff : array 1 element is cart_id new
             **/
            while(true){
                $arrNumRand = array(rand(1,2147483647));
                $arrDiff = array_diff($arrNumRand, $arrCarts);
                if($arrDiff){
                    break;
                }
            }
            $cart_id = $arrDiff[0];
            $created_date = date('Y-m-d');
            $cart = new Shoppingcart_Models_Cart(array(
                        'cart_id'      => $cart_id,
                        'created_date' => $created_date,
            ));
            $bool = $cartDao->add($cart);

            /**
            * Set cookie new value if add cart success
            **/
            if($bool){
                setcookie('cart_cookie', $cart_id, time()+60*60*24*2, '/');
            }
        }else{
            $cart_id = $_COOKIE['cart_cookie'];

            /**
             * Check $cart_id isset database
             * If not exist in database: Add to database and set new cookie
            **/
            $cart = $cartDao->getById($cart_id);
            if(!$cart){
                $cart_id = $cart_id;
                $created_date = date('Y-m-d');
                $cart = new Shoppingcart_Models_Cart(array(
                            'cart_id'      => $cart_id,
                            'created_date' => $created_date,
                ));
                $bool = $cartDao->add($cart);

                /**
                * Set cookie new value if add cart success
                **/
                if($bool){
                    setcookie('cart_cookie', $cart_id, time()+60*60*24*2, '/');
                }
            }else{
                if(isset($_SESSION['time_for_update'])){
                    $time_for_update = $_SESSION['time_for_update'];
                    if(time() - $time_for_update >= 3600){
                        /**
                        * Update date active for cart_id
                        **/
                        $created_date = date('Y-m-d');
                        $cart = new Shoppingcart_Models_Cart(array(
                                    'cart_id'      => $cart_id,
                                    'created_date' => $created_date,
                        ));
                        $returnId = $cartDao->update($cart);

                        $_SESSION['time_for_update'] = time();
                    }
                }else{
                    $_SESSION['time_for_update'] = time();
                }
            }
        }
        return $cart_id;
    }
}
